package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.LoginVo;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 登录方法
     * @param loginVo
     * @return
     */
    @Override
    public UserInfo login(LoginVo loginVo) {
        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(userInfoLambdaQueryWrapper -> {
            userInfoLambdaQueryWrapper.eq(UserInfo::getLoginName, loginVo.getLoginName())
                    .or().eq(UserInfo::getEmail, loginVo.getLoginName())
                    .or().eq(UserInfo::getPhoneNum, loginVo.getLoginName());
        });
        String newPwd = MD5.encrypt(loginVo.getPasswd());
        lambdaQueryWrapper.eq(UserInfo::getPasswd, newPwd);
        UserInfo userInfo = userInfoMapper.selectOne(lambdaQueryWrapper);
        return userInfo;
    }
}
